//
//  PersonalDataVC.m
//  BigJoy
//
//  Created by Rowling on 2017/7/3.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "PersonalDataVC.h"
#import "PersonInfoCell.h"
#import "choseTakePhotoVIew.h"
#import "PersonalViewModel.h"
#import "XMPikerView.h"
#import "DesignPreferencesVC.h"
#import "InputView.h"
#import "CitySelectView.h"

@interface PersonalDataVC ()
@property(nonatomic,strong)UIView * headView;
@property (nonatomic,strong) PersonalViewModel*viewModel;

@end

@implementation PersonalDataVC
-(PersonalViewModel *)viewModel {
    if (_viewModel == nil) {
        _viewModel = [[PersonalViewModel alloc]init];
        [_viewModel bindView:self.view];
    }
    return _viewModel;
}
// tableView的头部视图
-(UIView *)headView {
    if (_headView == nil) {
        _headView = [[UIView alloc]init];
        _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80);
        UILabel * label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:12];
        label.text = @"头像";
        label.textColor = [UIColor colorForHex:@"999999"];
        [_headView addSubview:label];
        [label sizeToFit];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_headView).offset(15);
            make.centerY.equalTo(_headView);
        }];
        UIButton * headImageBtn = [UIButton new];
        [_headView addSubview:headImageBtn];
        [headImageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_headView).offset(-15);
            make.centerY.equalTo(_headView);
            make.width.height.equalTo(@66);
        }];
        headImageBtn.backgroundColor = [UIColor redColor];
        [XMSuperHelper clipCircleFromeView:headImageBtn withRadius:33];
        @weakify(self);
        [[headImageBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self.viewModel showTheChosePhotoView];
        }];
        
        // 下面的一条线
        UIView * lineView = [UIView new];
        lineView.backgroundColor = XMColor(222, 222, 222);
        [_headView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(_headView);
            make.left.equalTo(_headView).offset(15);
            make.height.equalTo(@1);
        }];
    }
    return _headView;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

-(void)setUI {
    self.title = @"个人资料";
    self.baseDataArray = @[
                           @{@"title" : @"昵称",@"img" : @""},
                           @{@"title" : @"认证",@"img" : @""},
                           @{@"title" : @"个性签名",@"img" : @""},
                           @{@"title" : @"性别",@"img" : @""},
                           @{@"title" : @"生日",@"img" : @""},
                           @{@"title" : @"邮箱",@"img" : @""},
                           @{@"title" : @"所在地",@"img" : @""},
                           @{@"title" : @"注册时间",@"img" : @""},
                           @{@"title" : @"公司",@"img" : @""},
                           @{@"title" : @"标签",@"img" : @""},
                           @{@"title" : @"简介",@"img" : @""},
                           ];
//    [self.baseMutableDataArray addObjectsFromArray:@[@"",@"",@"",@"",@"",@"",@"",@"",@""]];
    [self.view addSubview:self.XMTableview];
    self.XMTableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.XMTableview.tableHeaderView = self.headView;
    self.XMTableview.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 50);
    // bottomView
    UIView * bottomView = [UIView new];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.equalTo(@50);
    }];
    UIButton * btn = [UIButton new];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setBackgroundColor:XMColor(245, 70, 70)];
    [bottomView addSubview:btn];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bottomView).offset(-15);
        make.centerY.equalTo(bottomView);
        make.height.equalTo(@30);
        make.width.equalTo(@90);
    }];

    [XMSuperHelper clipCircleFromeView:btn withRadius:15];
    UIView * lineView = [UIView new];
    lineView.backgroundColor = XMColor(220, 220, 220);
    [bottomView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(bottomView);
        make.height.equalTo(@1);
    }];
    bottomView.backgroundColor = [UIColor whiteColor];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.baseDataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonInfoCell * cell = [PersonInfoCell cellWithTalbelView:tableView];
    cell.data = self.baseDataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        // 昵称
        [self showTheInputView];
    }
    if (indexPath.row == 3) {
        // 性别
        [self choseSex];
    }
    if (indexPath.row == 4) {
        // 生日
        [self choseBirthday];
    }
    if(indexPath.row == 6) {
        [self showCityselectView];
    }
    if (indexPath.row == 9) {
        //设计偏好
        [self choseDesignPerference];
    }
}
// 弹出选择生日
-(void)choseBirthday {
    XMPikerView * view = [[XMPikerView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT) AndType:BirthDate];
    [XMKEYWINDOWXM addSubview:view];
}
//弹出选择性别
-(void)choseSex {
    choseTakePhotoVIew * choseView = [[choseTakePhotoVIew alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) titleArray:@[@"男",@"女"]];
//    @weakify(self);
    [choseView.subject subscribeNext:^(id  _Nullable x) {
        long index = [x longValue];
//        @strongify(self);
        if (index == 0) {
            
        }
        if (index == 1) {
            
        }
    }];
    [XMKEYWINDOWXM addSubview:choseView];
}

// 进入设计偏好界面
-(void)choseDesignPerference{
    DesignPreferencesVC * vc = [DesignPreferencesVC new];
    [self presentViewController:vc animated:YES completion:nil];
}
// 输入界面
-(void)showTheInputView {
    InputView * view = [InputView fastLogin];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [XMKEYWINDOWXM addSubview:view];
}

//地址选择
-(void)showCityselectView {
    CitySelectView * cityView = [CitySelectView fastLogin];
    cityView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [cityView setAdBlock:^(NSString * str){
        NSLog(@"%@",str);
    }];
    [XMKEYWINDOWXM addSubview:cityView];
    cityView.noArea = YES;
}

@end
